const $api = require('./../../utils/request').API
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'jinri',
    showTimePicker: false,
    showTime: false,
    timeType: '',
    startDate: '',
    overData: '',
    info: {},
    minHour: 10,
    maxHour: 20,
    minDate: new Date(2019, 10, 1).getTime(),
    maxDate: new Date(2030, 10, 1).getTime(),
    currentDate: new Date().getTime(),
  },

  haldClick(e) {
    this.setData({
      type: e.currentTarget.dataset.type
    })
    if (e.currentTarget.dataset.type == 'jinri' || e.currentTarget.dataset.type == 'zuori') {
      this.setData({
        showTime: false,
        startDate:'',
        overData:''
      });
      if (e.currentTarget.dataset.type == 'jinri') {
        var day2 = new Date();
        day2.setTime(day2.getTime());
        var s1 = day2.getFullYear() + "-" + ((day2.getMonth() + 1) < 10 ? '0' + (day2.getMonth() + 1) : (day2.getMonth() + 1)) + "-" + (day2.getDate() >= 10 ? day2.getDate() : '0' + day2.getDate())
        var s2 = day2.getFullYear() + "-" + ((day2.getMonth() + 1) < 10 ? '0' + (day2.getMonth() + 1) : (day2.getMonth() + 1)) + "-" + (day2.getDate() >= 10 ? day2.getDate() : '0' + day2.getDate())
        this.getData(s1, s2);
      } else {
        var day1 = new Date();
        day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
        var s1 = day1.getFullYear() + "-" + ((day1.getMonth() + 1) < 10 ? '0' + (day1.getMonth() + 1) : (day1.getMonth() + 1)) + "-" + (day1.getDate() >= 10 ? day1.getDate() : '0' + day1.getDate());
        var s2 = day1.getFullYear() + "-" + ((day1.getMonth() + 1) < 10 ? '0' + (day1.getMonth() + 1) : (day1.getMonth() + 1)) + "-" + (day1.getDate() >= 10 ? day1.getDate() : '0' + day1.getDate());
        this.getData(s1, s2);

      }

    } else {
      this.setData({
        showTime: true,
        info: {}
      })
    }
  },

  showpickerClick(event) {
    if( event.target.dataset.type == 'overData' &&  this.data.startDate == '' ){
      return wx.showToast({
        title: '请选择开始时间',
        icon:'none'
      })
    }
    this.setData({
      timeType: event.target.dataset.type,
      showTimePicker: true
    })
  },

  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  bindOverDateChange: function (e) {
    if (this.data.startDate == '') {
      return wx.showToast({
        title: '请选择开始时间',
        icon: 'none'
      })
    }
    this.setData({
      overData: e.detail.value
    });
    this.getData(this.data.startDate, this.data.overData);

  },


  getData(starttime, endtime) {
    if( !starttime  ||!endtime  ){
      return false
    }
    wx.showLoading({
      title: '正在加载',
    })
    let params = {
      "endTime": endtime,
      "gongHao": wx.getStorageSync('gongHao'),
      "startTime": starttime
    }
    $api.getDangTianYeJi(params).then(res => {
      wx.hideLoading({
        fail: () => { },
        success: () => { },
        complete: (complete) => { }
      })
      this.setData({
        info: res.data
      })
    }).catch(err => {
      wx.hideLoading({
        fail: () => { },
        success: () => { },
        complete: (complete) => { }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var day2 = new Date();
    day2.setTime(day2.getTime());
    var s1 = day2.getFullYear() + "-" + ((day2.getMonth() + 1) < 10 ? '0' + (day2.getMonth() + 1) : (day2.getMonth() + 1)) + "-" + (day2.getDate() >= 10 ? day2.getDate() : '0' + day2.getDate());
    var s2 = day2.getFullYear() + "-" + ((day2.getMonth() + 1) < 10 ? '0' + (day2.getMonth() + 1) : (day2.getMonth() + 1)) + "-" + (day2.getDate() >= 10 ? day2.getDate() : '0' + day2.getDate());



    this.getData(s1, s2);
  },

  onconfirm() {
    if (this.data.timeType == 'startDate') {
      this.setData({
        startDate: this.timestampToTime(this.data.currentDate) 
      })
    } else {
      this.setData({
        overData: this.timestampToTime(this.data.currentDate) 
      });
     
    }
    this.setData({
      showTimePicker: false
    });
    this.getData(this.data.startDate, this.data.overData);
  },

  oncancel() {
    this.setData({
      showTimePicker: false
    });
  },

 timestampToTime(timestamp) {

    var date = new Date(timestamp);

    var Y = date.getFullYear() + '-';

    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';

    var D = date.getDate() + ' ';

    var h = date.getHours() + ':';

    var m = date.getMinutes()<10?  '0'+date.getMinutes()+ ':':date.getMinutes() + ':';

    var s = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds() ;

    return Y+M+D+h+m+s;

},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})